package com.longmao.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longmao.commonutils.JwtUtils;
import com.longmao.commonutils.MD5;
import com.longmao.educenter.entity.UcenterMember;
import com.longmao.educenter.entity.vo.RegisterVo;
import com.longmao.educenter.mapper.UcenterMemberMapper;
import com.longmao.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longmao.servicebase.handler.LongMaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author longmao
 * @since 2022-02-21
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember ucenterMember) {
        String phone = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        //进行非空判断
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) throw  new LongMaoException(20001, "登录失败!");

        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", phone);
        UcenterMember member = baseMapper.selectOne(memberQueryWrapper);
        //判断查询对象是否为空
        if(StringUtils.isEmpty(member)) throw  new LongMaoException(20001, "该用户不存在!");
        //判断密码
        if(!MD5.encrypt(password).equals(member.getPassword())) throw  new LongMaoException(20001, "密码错误!");
        //判断是否被禁用
        if(member.getIsDisabled()) throw  new LongMaoException(20001, "该用户被禁用!");

        //登录成功，生成token
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        //获取注册得数据
        String phone = registerVo.getMobile();
        String code = registerVo.getCode();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //进行非空判断
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) throw  new LongMaoException(20001, "注册失败!");

        //验证验证码
        String redisCode = redisTemplate.opsForValue().get(phone);
        if (!redisCode.equals(code)) throw  new LongMaoException(20001, "验证码已失效!");

        //验证手机号是否重复
        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", phone);
        Integer count = baseMapper.selectCount(memberQueryWrapper);
        if(count > 0) throw  new LongMaoException(20001, "改手机号已注册!");

        //添加到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(phone);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);     //是否被禁用
        ucenterMember.setAvatar("http://yun-edu-long.oss-cn-beijing.aliyuncs.com/2022-02-18/51e7299ef96149f38d816a88ed143efbbns.jpg");
        Integer result = baseMapper.insert(ucenterMember);
        return null != result && result > 0;
    }

    /**
     * 根据openid 进行查询
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(memberQueryWrapper);
        return member;
    }

    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.selectRegisterCount(day);
    }
}
